<?php
/*
 * student控制器  
 * Tobey
 * */
require_once __ROOT__."/tayphp/Controller.class.php";
class studentController extends Controller{

	/**
	 * 修改学生的基本信息
	 * ajax     stu_name 	sex
	 */
	public function update_info(){
		session_start();
        if(IS_AJAX && IS_LOCAL && isset($_SESSION["id"])) {       //需要stu_name sex
        	$sql = D();
        	if($sql->table(array("student"=>""))->check($_POST,2)){
        		if($sql->table(array("student"=>""))->where(array("stu_id"=>$_SESSION["id"]))->update($_POST)){
        			$data = $this->message(1,"信息修改成功!","");
        		} else{
        			$data = $this->message(0,"信息没有任何变化,无需修改!","");
        		}
        	} else{
        		$data = $this->message(0,$sql->getCheckError(),"");
        	}
        	unset($sql);
        } else{
        	$data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        session_write_close();
        unset($data);
    }

    /**
     * 学生修改密码
     * ajax  		old_pwd   new_pwd
     */
    public function update_pwd(){
    	session_start();
        if(IS_AJAX && IS_LOCAL && isset($_SESSION["id"])) {           //需要old_pwd   new_pwd
        	$where["stu_id"] = $_SESSION["id"];
        	$sql = D();
        	$login_stu = $sql->table(array("student"=>""))->where($where)->field(array("stu_password_hash"))->find();
        	if($login_stu && $login_stu["stu_password_hash"] == md5($_POST["old_pwd"])){
        		if(!empty($_POST["new_pwd"])){
        			if($sql->table(array("student"=>""))->where($where)->update(array("stu_password_hash"=>md5($_POST["new_pwd"])))){
        				$data = $this->message(1,"密码修改成功!","");
        			} else{
        				$data = $this->message(0,"新密码与旧密码一样,无需修改!","");
        			}
        		} else{
        			$data = $this->message(0,"新密码不能为空!","");
        		}
        	} else{
        		$data = $this->message(0,"旧密码错误,无法修改!","");
        	}
        	unset($sql,$login_stu,$where);
        } else{
        	$data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        unset($data);
        session_write_close();
    }

    /**
     * 学生注册
     * ajax
     */
    public function add(){
        if(IS_AJAX && IS_LOCAL) {           //需要的信息:stu_number  stu_password_hash  stu_name   stu_email   col_id
        	$sql = D();
        	if($sql->table(array("student"=>""))->check($_POST,1)){
        		if($sql->table(array("student"=>""))->where(array("stu_email"=>$_POST["stu_email"]))->count() == 0 && $sql->table(array("teacher"=>""))->where(array("tea_email"=>$_POST["stu_email"]))->count() == 0 && $sql->table(array("student"=>""))->where(array("stu_number"=>$_POST["stu_number"],"col_id"=>$_POST["col_id"]))->count() == 0){
        			if($sql->table(array("student"=>""))->add($_POST)){
        				$data = $this->message(1,"学生注册成功!","");
        			} else{
        				$data = $this->message(0,"学生注册失败!","");
        			}
        		} else{
        			$data = $this->message(0,"邮箱或者学号已经存在(被注册)!","");
        		}
        	} else{
        		$data = $this->message(0,$sql->getCheckError(),"");
        	}
        	unset($sql);
        } else{
        	$data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        unset($data);
    }

    /**
     * 检查学号是否已经存在
     * ajax
     */
    public function check_number(){
        if(IS_AJAX && IS_LOCAL) {               //需要col_id  stu_number
        	if(D()->table(array("student"=>""))->where(array("stu_number"=>$_POST["stu_number"],"col_id"=>$_POST["col_id"]))->count() == 0){
        		$data = $this->message(1,"该学号可以进行注册使用!","");
        	} else{
        		$data = $this->message(0,"该学号已经被注册使用了!","");
        	}
        } else{
        	$data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        unset($data);
    }

    /**
     * 根据学号查询某个学生信息
     * ajax   stu_number
     */
    public function getStuInfoByNumber(){
        if(IS_AJAX && IS_LOCAL && isset($_POST["stu_number"])){
            $sql = D();
            session_start();
            $teacher = $sql->table(array("teacher"=>""))->where(array("tea_id"=>$_SESSION["id"]))->field(array("college_col_id"))->find();
            session_write_close();
            $stuInfo = D()->table(array("student"=>""))->where(array("stu_number"=>$_POST["stu_number"],"col_id"=>$teacher["college_col_id"]))->field(array("stu_id", "stu_number", "stu_name", "stu_email"))->find();
            if($stuInfo){
                $data = $this->message(1,"查询到该学生!",$stuInfo);
            } else{
                $data = $this->message(0,"查询不到该学生","");
            }
            unset($stuInfo,$teacher,$sql);
        } else{
            $data = $this->message(0,"非法操作!!","");
        }
        $this->ajaxReturn($data);
        unset($data);
    }

    /**
     * 根据number或者名称模糊查询
     * ajax    cla_id   keyword
     */
    public function search(){
        if(IS_AJAX && IS_LOCAL){
            require_once "baseController.class.php";
            $base = new baseController();
            $data = $base->classPower();
            if($data["status"] == 1) {
                $data = $this->message(1,"查询成功!",D()->table(array("student"=>"stu","class_student"=>"cs"))->where(array("cs.class_cla_id"=>$_POST["cla_id"],"stu.stu_number|stu.stu_name"=>array("like","%".$_POST["keyword"]."%"),"cs.is_pass"=>1,"__string__"=>"cs.student_stu_id = stu.stu_id"))->field(array("stu.stu_id","stu.stu_number","stu.stu_name","stu.stu_email","stu.sex"))->select());
            }
            $this->ajaxReturn($data);
            unset($data);
        }
    }

	//析构函数不用写任何内容也是可以的
    public function __destruct(){}
}